package com.woniu.springboot.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.woniu.springboot.dao.TeacherDao;
import com.woniu.springboot.entity.Teacher;

@Repository
public class TeacherDaoImpl implements TeacherDao {
	@PersistenceContext
	EntityManager entityManager;

	@Override
	public void insertTeacher(Teacher teacher) {
		entityManager.persist(teacher);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Teacher> selectTeachers() {
		String hql = "from teacher";
		return (List<Teacher>) entityManager.createQuery(hql).getResultList();
	}

	@Override
	public Teacher selectTeacherByTid(Integer tid) {
		return entityManager.getReference(Teacher.class, tid);
	}

	@Override
	public void deleteTeacherByTid(Integer tid) {
		Teacher teacher = selectTeacherByTid(tid);
		entityManager.remove(teacher);
	}

	@Override
	public void updateStudentByTid(Teacher teacher) {
		Teacher teache = selectTeacherByTid(teacher.getTid());
		teache.setTname(teacher.getTname());
		entityManager.flush();
	}

}
